package com.zhangzeqin.code.dao.Impl;

import com.zhangzeqin.code.dao.AdminDao;
import com.zhangzeqin.code.entity.Admin;
import com.zhangzeqin.code.entity.Books;
import com.zhangzeqin.code.entity.User;
import com.zhangzeqin.code.entity.Word;
import com.zhangzeqin.code.util.DruidTool;
import com.zhangzeqin.code.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

public class AdminDaoImpl implements AdminDao {


    @Override
    public Object[][] listUser() {
        String sql="select * from user;";
        Object [][] arr = new Object[100][11];
        //作为数组第一维的索引
        int i=0;
        List<User>list=null;
        try {
            list=JdbcUtil.selectAll(User.class,sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        Iterator iterator=list.iterator();
        while(iterator.hasNext()){
            User user= (User) iterator.next();
            //给二维数组每个i赋值
            getArr(arr, i, user);
            i++;
        }
        return arr;
    }

    private void getArr(Object[][] arr, int i, User user) {
        arr[i][0]= user.getId();
        arr[i][1]= user.getName();
        arr[i][2]= user.getPassWord();
        arr[i][3]= user.getNickName();
        arr[i][4]= user.getGender();
        arr[i][5]= user.getBirth();
        arr[i][6]= user.getNumber();
        arr[i][7]= user.getClockDay();
        arr[i][8]= user.getStudyTime();
        arr[i][9]= user.getWordNumber();
        arr[i][10]= user.getAimWords();
    }

    @Override
    public boolean addBook(String bookName,String nature) {
        String sql="create table "+ nature +" (word varchar(20),meaning varchar(20))";
        String sql1="insert into books values(?,?);";
        //连接数据库
        boolean flag=false;
        int row=0;
        try {
            flag = JdbcUtil.creatTbale(sql);
            row=JdbcUtil.executeUpdate(sql1,nature,bookName);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if(flag==true&&row!=0){
            return true;
        }
        return false;
    }

    @Override
    public boolean landing(String name, String password ,String table) {
        String sql = "select * from "+table+" where name=? and pass_word=?;";
        //连接数据库
        boolean flag=false ;
        try {
            flag = JdbcUtil.Query(sql, name, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return flag;
    }

    @Override
    public Object[] nature() {
        String sql="select * from books;";
        Object []arr = new Object[100];
        //作为数组第一维的索引
        int i=0;
        List<Books>list=null;
        try {
            list=JdbcUtil.selectAll(Books.class,sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        Iterator iterator=list.iterator();
        while(iterator.hasNext()){
            Books books= (Books) iterator.next();
            //给二维数组每个i赋值
            natureArr(arr,i,books);
            i++;
        }
        return arr;
    }

    @Override
    public Object[][] listBook() {
        String sql="select * from books;";
        Object [][] arr1 = new Object[100][2];
        //作为数组第一维的索引
        int i=0;
        List<Books>list=null;
        try {
            list=JdbcUtil.selectAll(Books.class,sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        Iterator iterator=list.iterator();
        while(iterator.hasNext()){
            Books books= (Books) iterator.next();
            //给二维数组每个i赋值
            bookArr(arr1, i, books);
            i++;
        }
        return arr1;
    }



    @Override
    public Object[][] listWord(String bookName) {
        String sql="select * from "+bookName+";";
        Object [][] arr = new Object[100][2];
        //作为数组第一维的索引
        int i=0;
        List<Word>list=null;
        try {
            list=JdbcUtil.selectAll(Word.class,sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        Iterator iterator=list.iterator();
        while(iterator.hasNext()){
            Word word=(Word) iterator.next();
            //给二维数组每个i赋值
            wordArr(arr, i, word);
            i++;
        }
        return arr;
    }

    @Override
    public Object getNotice() throws SQLException{
        String sql="select notice from admin;";
        Object notice=null ;
        //获取连接
        Connection connection = DruidTool.getConnection();
        //创建预编译statement
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //返回结果集
        ResultSet resultSet = preparedStatement.executeQuery(sql);
        while(resultSet.next()) {
            notice = (String) resultSet.getObject(1);
        }
        if (resultSet.next()){
            //是否收回连接,需要考虑是不是事务
            if(connection.getAutoCommit()){
                //没有开启事务,正常回收连接
                DruidTool.recycleConnection(connection);
            }
            resultSet.close();
        }


        return notice;
    }

    @Override
    public boolean publishNotice(String notice) {
        String sql="update admin set notice = ? where name ='admin';";
        //连接数据库
        int row=0;
        try {
            row = JdbcUtil.executeUpdate(sql, notice);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if(row!=0){
            return true;
        }
        return false;

    }


    private void bookArr(Object[][] arr1, int i, Books books) {
        arr1[i][0]= books.getNature();
        arr1[i][1]= books.getBookName();
    }
    //重载
    private void wordArr(Object[][] arr1, int i, Word word) {
        arr1[i][0]= word.getWord();
        arr1[i][1]= word.getMeaning();
    }

    private void natureArr(Object[] arr, int i, Books books) {
        arr[i]=books.getNature() ;

    }


}
